package com.pickerview.lib;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

import android.util.Log;
import android.view.View;

import com.pickerview.R;
import com.pickerview.lib.listener.OnWheelChangedListener;

public class WheelOptions {
  private View view;
  private WheelView wv_option1;
  private WheelView wv_option2;
  private WheelView wv_option3;

  private List<String> mOptions1Items;
  private List<String> mOptions3ItemLs;
  private List<String> mOptions2ItemLs;

  private List<List<String>> mOptions2Items;
  private List<List<List<String>>> mOptions3Items;
  public int screenheight;

  public View getView() {
    return view;
  }

  public void setView(View view) {
    this.view = view;
  }

  public WheelOptions(View view) {
    super();
    this.view = view;
    setView(view);
  }

  public void setPicker(List<String> optionsItems) {
    setPicker(optionsItems, null, null, false);
  }

  public void setPicker(List<String> options1Items, List<List<String>> options2Items, boolean linkage) {
    setPicker(options1Items, options2Items, null, linkage);
  }

  /**
   * 两个独立数组
   * 
   * @param options1Items
   * @param options2Items
   */
  @SuppressWarnings("unchecked")
  public void setPicker(List<String> options1Items, List<String> mOptions2ItemLs, List<String> mOptions3ItemLs) {
    this.mOptions1Items = options1Items;
    this.mOptions2ItemLs = mOptions2ItemLs;
    this.mOptions3ItemLs = mOptions3ItemLs;
    int len = ArrayWheelAdapter.DEFAULT_LENGTH;
    if (this.mOptions2ItemLs == null)
      len = 8;
    if (this.mOptions3ItemLs == null)
      len = 12;

    List<Integer> maxLs = new ArrayList<Integer>();
    if (!options1Items.isEmpty()) {
      for (int i = 0; i < options1Items.size(); i++) {
        maxLs.add(options1Items.get(i).toString().length());
      }
      len = Collections.max(maxLs);
    }
    
    // 选项1
    wv_option1 = (WheelView) view.findViewById(R.id.options1);
    wv_option1.setAdapter(new ArrayWheelAdapter(mOptions1Items, len+4));// 设置显示数据
    wv_option1.setCurrentItem(0);// 初始化时显示的数据
    // 选项2
    wv_option2 = (WheelView) view.findViewById(R.id.options2);
    if (mOptions2ItemLs != null)
      wv_option2.setAdapter(new ArrayWheelAdapter(mOptions2ItemLs));// 设置显示数据
    wv_option2.setCurrentItem(0);// 初始化时显示的数据
    // 选项3
    wv_option3 = (WheelView) view.findViewById(R.id.options3);
    if (mOptions3ItemLs != null)
      wv_option3.setAdapter(new ArrayWheelAdapter(mOptions3ItemLs));// 设置显示数据
    wv_option3.setCurrentItem(0);// 初始化时显示

    int textSize = (screenheight / 100) * 3;

    wv_option1.TEXT_SIZE = textSize;
    wv_option2.TEXT_SIZE = textSize;
    wv_option3.TEXT_SIZE = textSize;

    if (this.mOptions2ItemLs == null)
      wv_option2.setVisibility(View.GONE);
    if (this.mOptions3ItemLs == null)
      wv_option3.setVisibility(View.GONE);

  }

  @SuppressWarnings({ "unchecked", "rawtypes" })
  public void setPicker(List<String> options1Items, List<List<String>> options2Items,
      List<List<List<String>>> options3Items, boolean linkage) {
    this.mOptions1Items = options1Items;
    this.mOptions2Items = options2Items;
    this.mOptions3Items = options3Items;
    int len = ArrayWheelAdapter.DEFAULT_LENGTH;
    if (this.mOptions3Items == null)
      len = 8;
    if (this.mOptions2Items == null)
      len = 12;
    // 选项1
    List<Integer> maxLs = new ArrayList<Integer>();
    if (!options1Items.isEmpty()) {
      for (int i = 0; i < options1Items.size(); i++) {
        maxLs.add(options1Items.get(i).toString().length());
      }
      len = Collections.max(maxLs);
    }
    wv_option1 = (WheelView) view.findViewById(R.id.options1);
    wv_option1.setAdapter(new ArrayWheelAdapter(mOptions1Items,len+4));// 设置显示数据
    wv_option1.setCurrentItem(0);// 初始化时显示的数据
    // 选项2
    wv_option2 = (WheelView) view.findViewById(R.id.options2);
    if (mOptions2Items != null)
      wv_option2.setAdapter(new ArrayWheelAdapter(mOptions2Items.get(0),len));// 设置显示数据
    wv_option2.setCurrentItem(wv_option1.getCurrentItem());// 初始化时显示的数据
    // 选项3
    wv_option3 = (WheelView) view.findViewById(R.id.options3);
    if (mOptions3Items != null)
      wv_option3.setAdapter(new ArrayWheelAdapter(mOptions3Items.get(0).get(0),len));// 设置显示数据
    wv_option3.setCurrentItem(wv_option3.getCurrentItem());// 初始化时显示的数据

    // 根据屏幕密度来指定选择器字体的大小(不同屏幕可能不同)
    int textSize = (screenheight / 100) * 3;

    wv_option1.TEXT_SIZE = textSize;
    wv_option2.TEXT_SIZE = textSize;
    wv_option3.TEXT_SIZE = textSize;

    if (this.mOptions2Items == null)
      wv_option2.setVisibility(View.GONE);
    if (this.mOptions3Items == null)
      wv_option3.setVisibility(View.GONE);

    // 联动监听器
    OnWheelChangedListener wheelListener_option1 = new OnWheelChangedListener() {

      @Override
      public void onChanged(WheelView wheel, int oldValue, int newValue) {
        wv_option2.setAdapter(new ArrayWheelAdapter(mOptions2Items.get(wv_option1.getCurrentItem())));
        wv_option2.setCurrentItem(0);
        wv_option3.setAdapter(new ArrayWheelAdapter(mOptions3Items.get(wv_option1.getCurrentItem()).get(
            wv_option2.getCurrentItem())));
        wv_option3.setCurrentItem(0);
      }
    };
    OnWheelChangedListener wheelListener_option2 = new OnWheelChangedListener() {

      @Override
      public void onChanged(WheelView wheel, int oldValue, int newValue) {
        wv_option3.setAdapter(new ArrayWheelAdapter(mOptions3Items.get(wv_option1.getCurrentItem()).get(
            wv_option2.getCurrentItem())));
        wv_option3.setCurrentItem(0);
      }
    };

    // 添加联动监听
    if (options2Items != null && linkage)
      wv_option1.addChangingListener(wheelListener_option1);
    if (options3Items != null && linkage)
      wv_option2.addChangingListener(wheelListener_option2);
  }

  /**
   * 设置选项的单位
   * 
   * @param label1
   * @param label2
   * @param label3
   */
  public void setLabels(String label1, String label2, String label3) {
    if (label1 != null)
      wv_option1.setLabel(label1);
    if (label2 != null)
      wv_option2.setLabel(label2);
    if (label3 != null)
      wv_option3.setLabel(label3);
  }

  /**
   * 设置是否循环滚动
   * 
   * @param cyclic
   */
  public void setCyclic(boolean cyclic) {
    wv_option1.setCyclic(cyclic);
    wv_option2.setCyclic(cyclic);
    wv_option3.setCyclic(cyclic);
  }

  /**
   * 返回当前选中的结果对应的位置数组 因为支持三级联动效果，分三个级别索引，0，1，2
   * 
   * @return
   */
  public int[] getCurrentItems() {
    int[] currentItems = new int[3];
    currentItems[0] = wv_option1.getCurrentItem();
    currentItems[1] = wv_option2.getCurrentItem();
    currentItems[2] = wv_option3.getCurrentItem();
    return currentItems;
  }

  public void setCurrentItems(int option1, int option2, int option3) {
    wv_option1.setCurrentItem(option1);
    wv_option2.setCurrentItem(option2);
    wv_option3.setCurrentItem(option3);
  }

  public void setVisibleItems(int count) {
    wv_option1.setVisibleItems(count);
    wv_option2.setVisibleItems(count);
    wv_option3.setVisibleItems(count);
  }

}
